//
//  LGQuickReplyController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/28.
//  快捷回复

#import "LGQuickReplyController.h"
//添加、编辑快捷回复
#import "LGEditQuickReplyController.h"
@interface LGQuickReplyController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGQuickReplyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"管理快捷回复";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addButton];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/im/quickMessageList" parameters:@{@"keyword":@""} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无回复内容" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无回复内容" offset:0];
        }
    }];
}


//添加快捷回复
-(void)addButtonAction{
    LGEditQuickReplyController *controller = [[LGEditQuickReplyController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGQuickReplyCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    cell.content = LGNSString(self.dataArray[indexPath.row][@"content"]);
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGEditQuickReplyController *controller = [[LGEditQuickReplyController alloc]init];
    controller.replyId = LGNSString(tempDic[@"id"]);
    controller.content = LGNSString(tempDic[@"content"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(90)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGQuickReplyCell2 class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"添加" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@interface LGQuickReplyCell2()

/**   */
@property (nonatomic , strong)UILabel *contentLabel;

@end

@implementation LGQuickReplyCell2

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.lineView];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(viewPix(20));
            make.right.bottom.equalTo(self.contentView).offset(-viewPix(12));
            make.top.equalTo(self.contentView).offset(viewPix(12));
        }];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self.contentView);
            make.height.equalTo(@(0.5));
        }];
    }
    return  self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
    [self.contentLabel lineSpacing:3];
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#DFDFDF"];
    }
    return _lineView;
}

@end
